<?php
namespace Admin\Controller;
use Think\Controller;
class FinanceController extends CommonController {
	public function finance(){
		$this->display();
	}

	//订单列表
	public function get_orderlist(){
		$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
		// dump(I('excel'));
		// die;
        //搜索条件
        $map=array();
		
        $order_no=$ck->in('订单编码','order_no','ennumstr','',0,0);       
		$logistics_no=$ck->in('物流单号','logistics_no','ennumstr','',0,0);    
		$logiscompany_name=$ck->in('物流企业名称','logiscompany_name','ennumstr','',0,0);   		
        $user_name=$ck->in('客户姓名','user_name','cnennumstr','',0,0);
		$user_phone=$ck->in('联系电话','user_phone','ennumstr','',0,0);
		$start_time=I('start_time');
		$end_time=I('end_time');
		
		!empty($start_time)?$start_time = strtotime($start_time):$start_time=0;
		!empty($end_time)?$end_time = strtotime($end_time):$end_time =time();
		$map['_string'] = "add_time>=$start_time AND add_time<=$end_time";
		
		$is_delivery=I('is_delivery');
		if($is_delivery==0){//未选择
			$is_delivery='';
		}
		if(!empty($is_delivery)){
			if($is_delivery==2){
				$is_delivery=0;
			}
			$map['is_delivery']=$is_delivery;
		}
        !empty($order_no)?$map['order_no']=array('like','%'.$order_no.'%'):'';
		!empty($logistics_no)?$map['logistics_no']=array('like','%'.$logistics_no.'%'):'';
		!empty($user_name)?$map['user_name']=array('like','%'.$user_name.'%'):'';
		!empty($user_phone)?$map['user_phone']=array('like','%'.$user_phone.'%'):'';
		
		
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }
		// echo $m->getlastsql();
		// die;
		$excel=I('excel');
		$count_avg=0;
		if(!empty($excel)){ //导出Excel
			foreach($info as $k=>$v){
				$info[$k]['user_phone']="'".$v['user_phone'];
				$info[$k]['logistics_no']="'".$v['logistics_no'];
				$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
				
			}
		}else{
			foreach($info as $k=>$v){
				$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
				//汇总
				$total_price_count+=$v['total_price'];
				$finance_total_price_count+=$v['finance_total_price'];
				$factory_price_count+=$v['factory_price'];
				$finance_factory_price_count+=$v['finance_factory_price'];
				$count_avg=$k+1;
			}
		}
		//平均
		$footer[0]['is_delivery']='2';
		$footer[0]['order_no']='平均';
		$footer[0]['total_price']=$total_price_count/$count_avg;
		$footer[0]['finance_total_price']=$finance_total_price_count/$count_avg;
		$footer[0]['factory_price']=$factory_price_count/$count_avg;
		$footer[0]['finance_factory_price']=$finance_factory_price_count/$count_avg;
		
		$footer[1]['is_delivery']='2';
		$footer[1]['order_no']='汇总';
		$footer[1]['total_price']=$total_price_count;
		$footer[1]['finance_total_price']=$finance_total_price_count;
		$footer[1]['factory_price']=$factory_price_count;
		$footer[1]['finance_factory_price']=$finance_factory_price_count;
		// dump($info);
		// die;
		if(!empty($excel)){ //导出Excel
			$xlsName  = "财务查询";
			$xlsCell  = array(
				array('id','ID'),
				array('order_no','订单编号'),
				array('user_name','客户姓名'),
				array('user_phone','联系电话'),
				array('logistics_no','物流单号'),
				array('logiscompany_name','物流商家'),
				array('add_time','推送时间'),
				array('goods_name','商品名称'),
				array('goods_nums','数量'),
				array('goods_price','单价'),
				array('total_prices','总价')
			);
			exportExcel($xlsName,$xlsCell,$info);
			die;
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
			$data['footer']=$footer;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		// dump($data);
        $this->ajaxReturn($data);
	}

	//更新财务编辑订单信息
    public function financeSave(){
		$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['finance_total_price']=I('finance_total_price');
		$data['finance_factory_price']=I('finance_factory_price');
		$data['finance_goods_tax']=I('finance_goods_tax');
		
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
}